package main

import (
	"runtime"
	"strconv"
	"sync"
	"time"
)

var (
	lock sync.Mutex //声明全局锁(Mutex互斥)
)

func test() {
	lock.Lock()
	for i := 0; i < 10; i++ {
		println("test" + strconv.Itoa(i))
		time.Sleep(time.Second / 2)
	}
	lock.Unlock()
}
func main() {
	//协程
	//独立栈空间
	//共享堆空间
	//由用户控制
	//由main扩展而来轻量级  主线程结束所有协程也会提前结束
	go test() //开启协程
	go test() //开启协程
	for i := 0; i < 10; i++ {
		println("main" + strconv.Itoa(i))
		time.Sleep(time.Second)
	}

	//设置cpu运行数量
	cpNum := runtime.NumCPU()
	println(cpNum)
	runtime.GOMAXPROCS(cpNum - 1) //最大值
	println(cpNum)

}
